﻿<%@ WebHandler Language="C#" Class="EmpSettingMgr" %>

using System;
using System.Web;
using System.Text;
using System.Collections.Generic;
using System.Data;
using Chenxin.Lib.Utility;

public class EmpSettingMgr : AjaxHandlerBase
{
    public EmpSettingMgr()
    {
        htTypePermit.Add("del", Constants.Permit_DepartUserDel);
        htTypePermit.Add("delbydep", Constants.Permit_DepartUserDel);
        htTypePermit.Add("edit", Constants.Permit_DepartUserUpdate);
    }

    public override void RequestProcess(HttpContext context)
    {
        context.Response.AddHeader("pragma", "no-cache");
        context.Response.AddHeader("cache-control", "");
        context.Response.CacheControl = "no-cache";

        string optype = context.Request["op"];
        switch (optype.ToLower())
        {
            case "getlist":
                GetList(context);       //查询数据列表
                break;
            case "getmodel":            //得到单条记录
                GetModel(context);
                break;
            case "del":             //删除设定
                Del(context);
                break;
            case "edit":          //更新设定
                Update(context);
                break;
            case "delbydep":        //删除部门非隶属设定
                DelByDep(context);
                break;
            case "empsetting":         //添加人员业务权限
                addUser(context);
                break;
            case "getuserbycode":       //员工工号验证
                getUserByCode(context);
                break;
        }        
    }


    //根据用户工号得到用户信息
    private void getUserByCode(HttpContext context)
    {
        try
        {
            string usercode = context.Request.QueryString["usercode"];
            Chenxin.Lib.Passport.Entity.Hr_UserInfo userinfo = Chenxin.Lib.Passport.DAL.Hr_UserInfo.GetEntityByCode(usercode);
            if (userinfo != null)
            {
                if (userinfo.Status == 1)
                    context.Response.Write(string.Format("{{sucess:1,msg:'',data:{{exist:true,userinfo:'{0}',UserId:'{1}'}}}}", userinfo.UserName + "(" + userinfo.BelongDepart + ")", userinfo.UserId.ToString()));
                else
                    context.Response.Write(string.Format("{{success:'1',msg:'',data:{{exist:false}}}}"));
            }
            else
                context.Response.Write(string.Format("{{success:'1',msg:'',data:{{exist:false}}}}"));
        }
        catch (Exception ex)
        {
            context.Response.Write(string.Format("{{success:'0',msg:'意外错误：{0}',data:''}}", ex.Message));
        }
    }
    
    //删除部门非隶属设定
    private void DelByDep(HttpContext context)
    {
        try
        {
            string departid = context.Request.Params["departid"];
            string strSql = string.Format("delete t_Business_EmpSetting where DepartId='{0}' and IsBelong=0", departid);
            Chenxin.Lib.Business.DAL.t_Business_EmpSetting.ExecuteSQL(strSql);

            //AddLog("" + CurrentUserInfo.UserName + "删除业务设定",
            //        OperationType.OperationTypeComboList()["OprType_DelEMPSetting"].ToString());
            
            context.Response.Write(string.Format("{{success:'1',msg:'删除成功',data:''}}"));
        }
        catch (Exception ex)
        {
            context.Response.Write(string.Format("{{success:'0',msg:'{0}',data:''}}", ex.Message));
        }
    }
    
    
    //更新设定
    private void Update(HttpContext context)
    {
        try
        {
            Chenxin.Lib.Business.Entity.t_Business_EmpSetting model = (Chenxin.Lib.Business.Entity.t_Business_EmpSetting)JsonHelp.Convert2Entity(context.Request["model"], typeof(Chenxin.Lib.Business.Entity.t_Business_EmpSetting));
            if (model.IsBelong)
                model.IsControl = true;
            Chenxin.Lib.Business.DAL.t_Business_EmpSetting.Update(model);

            //AddLog("" + CurrentUserInfo.UserName + "编辑业务设定:员工姓名[" + model.UserName + "(" + model.UserCode + ")]",
            //        OperationType.OperationTypeComboList()["OprType_EditEMPSetting"].ToString());
            
            context.Response.Write(string.Format("{{success:'1',msg:'编辑成功',data:''}}"));
        }
        catch (Exception ex)
        {
            context.Response.Write(string.Format("{{success:'0',msg:'{0}',data:''}}", ex.Message));
        }
    }
    
    
    //删除设定
    private void Del(HttpContext context)
    {
        try
        {
            string listid = context.Request.Params["modelid"];
           // listid = "'" + listid + "'";
            listid = listid.Replace(",", "','");
            string strSql = string.Format("delete t_Business_EmpSetting where RecordId in ('{0}') ;", listid);
            Chenxin.Lib.Business.DAL.t_Business_EmpSetting.ExecuteSQL(strSql);

            //AddLog("" + CurrentUserInfo.UserName + "删除业务设定",
            //       OperationType.OperationTypeComboList()["OprType_DelEMPSetting"].ToString());
            
            context.Response.Write(string.Format("{{success:'1',msg:'删除成功',data:''}}"));
        }
        catch (Exception ex)
        {
            context.Response.Write(string.Format("{{success:'0',msg:'{0}',data:''}}", ex.Message));
        }
    }
    

    //得到数据实体
    private void GetModel(HttpContext context)
    {
        try
        {
            string recordid = context.Request.Params["modelid"];
            //根据ID得到变动记录
            Chenxin.Lib.Business.Entity.t_Business_EmpSetting usersetting = Chenxin.Lib.Business.DAL.t_Business_EmpSetting.GetModel(new Guid(recordid));
            if (usersetting == null)
            {
                context.Response.Write(string.Format("{{success:'0',msg:'{0}',data:''}}", "参数错误，找不到对应的变动记录"));
            }
            context.Response.Write(string.Format("{{success:'1',msg:'成功',data:{{model:{0}}}}}",
                 Chenxin.Lib.Utility.JsonHelp.Convert2Json(usersetting)));
        }
        catch (Exception ex)
        {
            context.Response.Write(string.Format("{{success:'0',msg:'{0}',data:''}}", ex.Message));
        }
    }

    
    //查询数据列表
    private void GetList(HttpContext context)
    {
        try
        {
            //参数列表
            int iPageIndex = (string.IsNullOrEmpty(context.Request["pageindex"])) ? 1 : int.Parse(context.Request["pageindex"]) + 1;//页码
            int iPageSize = (string.IsNullOrEmpty(context.Request["pagesize"])) ? 30 : int.Parse(context.Request["pagesize"]);//页记录数

            //order文
            string strOrder = "t_Business_EmpSetting.DepartId desc,t_Business_EmpSetting.IsBelong desc,t_Business_EmpSetting.usercode asc";
            //查询表格
            string tablename = "t_Business_EmpSetting";
            //select文
            string TableFieldList = "*";
            string FieldList = "*";


            //权限
            string strwhere = "";

            Guid cityId = (string.IsNullOrEmpty(context.Request["cityid"])) ? Guid.Empty : new Guid(context.Request["cityid"]);
            Guid companyId = (string.IsNullOrEmpty(context.Request["companyid"])) ? Guid.Empty : new Guid(context.Request["companyid"]);
            Guid areaId = (string.IsNullOrEmpty(context.Request["areaid"])) ? Guid.Empty : new Guid(context.Request["areaId"]);
            Guid departId = (string.IsNullOrEmpty(context.Request["departid"])) ? Guid.Empty : new Guid(context.Request["departid"]);
            Guid userId = (string.IsNullOrEmpty(context.Request["userid"])) ? Guid.Empty : new Guid(context.Request["userid"]);
            string usercode = context.Request["usercode"];

            //判断权限范围/////////////////////////////////////////////
            if (CurrentUserInfo.ContainsPermit(Constants.Permit_UserSetting_All))
            {
                strwhere = " (1=1) ";
            }
            else if (CurrentUserInfo.ContainsPermit(Constants.Permit_UserSetting_City))
            {
                strwhere = string.Format(" (CityId='{0}') ", CurrentUserInfo.CityId.ToString());
            }
            else if (CurrentUserInfo.ContainsPermit(Constants.Permit_UserSetting_Company))
            {
                strwhere = string.Format(" (CompanyId='{0}') ", CurrentUserInfo.CompanyId.ToString());
            }
            else
                strwhere = " (1=0) ";

            if (departId != Guid.Empty)
                strwhere += string.Format(" and DepartId='{0}' ", departId.ToString());
            else if (areaId != Guid.Empty)
            {
                Chenxin.Lib.Hr.Entity.Hr_DepartmentInfo item = null;
                string strCachkey = string.Format(Chenxin.Lib.Cache.CacheDataType.GLOBAL_DEPARTMENT, areaId);
                Chenxin.Lib.Cache.CacheService cs = new Chenxin.Lib.Cache.CacheService(Chenxin.Lib.Cache.CacheConn.xCacheConn.GLOBAL);
                item = cs.GetObject<Chenxin.Lib.Hr.Entity.Hr_DepartmentInfo>(strCachkey);
                if (!cs.Hit)
                {
                    item = Chenxin.Lib.Hr.DAL.Hr_DepartmentInfo.GetModel(areaId);
                    if (item != null) cs.Set(strCachkey, item, DateTime.Now.AddDays(1));
                }

                if (item == null) context.Response.Write(string.Format("{{success:'0',msg:'{0}',data:''}}", "获取商圈或分类数据丢失"));

                strwhere += string.Format(" AND (DepartId in (select departId from dbo.v_Hr_DepartmentInfo where DepartSrc like '{0}%')) ", item.DepartSrc);
            }
            else if (companyId != Guid.Empty)
                strwhere += string.Format(" and CompanyId='{0}' ", companyId.ToString());
            else if (cityId != Guid.Empty)
                strwhere += string.Format(" and CityId='{0}' ", cityId.ToString());
            else
                strwhere += string.Format(" and CompanyId='{0}' ", CurrentUserInfo.CompanyId.ToString());
            if (userId != Guid.Empty)
                strwhere += string.Format(" and UserId='{0}' ", userId.ToString());
            if (!string.IsNullOrEmpty(usercode))
                strwhere += string.Format(" and usercode='{0}' ", usercode);


            DataSet list = Chenxin.Lib.Business.DAL.t_Business_EmpSetting.GetList(iPageIndex, iPageSize, strOrder, tablename, strwhere, FieldList, TableFieldList, true);

            //int count = (int)list.Tables[1].Rows[0][0];
            //string strHtml = "";
            
            //if (!GetKeyListHtml(list, out strHtml))
            //{
            //    context.Response.Write(string.Format("{{success:'0',msg:'{0}',data:''}}", "生成HTML错误:" + strHtml));
            //}
            //list.Dispose();
            //list = null;
            //string head = "{UserCode: {head: \"工号\",width: 100,sortable:true},UserName: {head: \"姓名\",width: 120,sortable:true},Company: {head: \"公司\",width: 80,sortable:true},DepartName: {head: \"部门\",width: 80,sortable:true},IsBelong: {width: 50,head: \"隶属\"},IsControl: {width: 50, head: \"管辖\"},SearchContractArchive: {width: 80, head: \"查询报备\"},EditContractArchive: {width: 80,head: \"编辑报备\"},CancelContractArchive: {width: 80, head: \"作废报备\"},SearchContractApply: {width: 190,head: \"查询业绩申请\"},EditContractApply: {width: 90, head: \"修改业绩申请\"}}";
            //context.Response.Write(string.Format("{{success:'1',msg:'{0}',data:{{count:'{1}',html:'{2}',head:{3}}}}}", "成功", count, strHtml, head));
            context.Response.Write(string.Format("{{success:'1',msg:'',data:{{count:{0},{1}}}}}", list.Tables[1].Rows[0][0], Chenxin.Lib.Utility.JsonHelp.Convert2Json(list.Tables[0])));
        }
        catch (Exception ex)
        {
            context.Response.Write(string.Format("{{success:'0',msg:'意外错误：{0}',data:''}}", ex.Message));
        }
    }

    private bool GetKeyListHtml(System.Data.DataSet dslist, out string strHtml)
    {
        try
        {
            System.Text.StringBuilder sb = new System.Text.StringBuilder();

            foreach (System.Data.DataRow row in dslist.Tables[0].Rows)
            {
                sb.AppendFormat("<dl id=\"{0}\" class=\"x-grid-row\">", row["RecordId"].ToString());
                sb.AppendFormat("<dd>{0}</dd>", Chenxin.Lib.Utility.DatasetHelper.GetValue(row, "UserCode", ""));
                sb.AppendFormat("<dd>{0}</dd>", Chenxin.Lib.Utility.DatasetHelper.GetValue(row, "UserName", ""));
                sb.AppendFormat("<dd>{0}</dd>", Chenxin.Lib.Utility.DatasetHelper.GetValue(row, "Company", ""));
                sb.AppendFormat("<dd>{0}</dd>", Chenxin.Lib.Utility.DatasetHelper.GetValue(row, "DepartName", ""));
                sb.AppendFormat("<dd>{0}</dd>", Chenxin.Lib.Utility.DatasetHelper.GetValue(row, "IsBelong", "").ToLower() == "true" ? "√" : "");
                sb.AppendFormat("<dd>{0}</dd>", Chenxin.Lib.Utility.DatasetHelper.GetValue(row, "IsControl", "").ToLower() == "true" ? "√" : "");
                sb.AppendFormat("<dd>{0}</dd>", Chenxin.Lib.Utility.DatasetHelper.GetValue(row, "SearchContractArchive", "").ToLower()=="true"?"√":"");
                sb.AppendFormat("<dd>{0}</dd>", Chenxin.Lib.Utility.DatasetHelper.GetValue(row, "EditContractArchive", "").ToLower() == "true" ? "√" : "");
                sb.AppendFormat("<dd>{0}</dd>", Chenxin.Lib.Utility.DatasetHelper.GetValue(row, "CancelContractArchive", "").ToLower() == "true" ? "√" : "");
                sb.AppendFormat("<dd>{0}</dd>", Chenxin.Lib.Utility.DatasetHelper.GetValue(row, "SearchContractApply", "").ToLower() == "true" ? "√" : "");
                sb.AppendFormat("<dd>{0}</dd>", Chenxin.Lib.Utility.DatasetHelper.GetValue(row, "EditContractApply", "").ToLower() == "true" ? "√" : "");
                sb.AppendFormat("<dd>{0}</dd>", Chenxin.Lib.Utility.DatasetHelper.GetValue(row, "SearchIntention", "").ToLower() == "true" ? "√" : "");
                sb.AppendFormat("<dd>{0}</dd>", Chenxin.Lib.Utility.DatasetHelper.GetValue(row, "SearchBargain", "").ToLower() == "true" ? "√" : "");
                sb.AppendFormat("<dd>{0}</dd>", Chenxin.Lib.Utility.DatasetHelper.GetValue(row, "SearchMoney", "").ToLower() == "true" ? "√" : "");
                sb.Append("</dl>");
            }
            strHtml = sb.ToString();
            return true;
        }
        catch (Exception ex)
        {
            strHtml = ex.Message;
            return false;
        }
    }

    //添加人员
    private void addUser(HttpContext context)
    {
        try
        {
            string stype = "";
            string todepid = "";    //目标门店ID
            string todepname = "";  //目标门店名称
            string depid = "";  //源对象
            string strSql = "";
            string qx = "";     //选中的权限 （列名）
            string tocompany = "";
            string tocompanyid = "";
            string tocity = "";
            string tocityid = "";
            string sub = "";    //添加的方式（重置、追加、剔除）
            string updatestr = "";  //更新的STR

            if (!string.IsNullOrEmpty(context.Request.Params["settingtype"]))
            {
                stype = context.Request.Params["settingtype"];
            }
            if (!string.IsNullOrEmpty(context.Request.Params["depid"]))
            {
                depid = context.Request.Params["depid"];
            }
            if (!string.IsNullOrEmpty(context.Request.Params["aimdepid"]))
            {
                todepid = context.Request.Params["aimdepid"];
            }
            if (!string.IsNullOrEmpty(context.Request.Params["aimdepname"]))
            {
                todepname = context.Request.Params["aimdepname"];
            }
            if (!string.IsNullOrEmpty(context.Request.Params["aimcityid"]))
            {
                tocityid = context.Request.Params["aimcityid"];
            }
            if (!string.IsNullOrEmpty(context.Request.Params["aimcity"]))
            {
                tocity = context.Request.Params["aimcity"];
            }
            if (!string.IsNullOrEmpty(context.Request.Params["aimcompanyid"]))
            {
                tocompanyid = context.Request.Params["aimcompanyid"];
            }
            if (!string.IsNullOrEmpty(context.Request.Params["aimcompany"]))
            {
                tocompany = context.Request.Params["aimcompany"];
            }
            if (!string.IsNullOrEmpty(context.Request.Params["sub"]))
            {
                sub = context.Request.Params["sub"];
            }


            if (!string.IsNullOrEmpty(context.Request.Params["authority"]))
            {
                qx = context.Request.Params["authority"] + ",";
            }
            if (!string.IsNullOrEmpty(context.Request.Params["purview"]))
            {
                qx += context.Request.Params["purview"];
                updatestr = context.Request.Params["purview"];
            }

            string str_qx = "";
            string[] array = qx.Split(',');
            for (int i = 0; i < array.Length; i++)
            {
                str_qx += "'1',";
            }

            string str_up_t = "";       //追加
            string str_up_f = "";       //剔除
            string[] array_up = updatestr.Split(',');
            for (int i = 0; i < array_up.Length; i++)
            {
                str_up_t += array_up[i] + "=1,";
                str_up_f += array_up[i] + "=0,";
            }

            string role = "";   //选中的角色    （角色id）
            if (!string.IsNullOrEmpty(context.Request.Params["role"]))
            {
                role = context.Request.Params["role"];
            }
            string[] todepid_array = todepid.Split(',');
            string[] todepname_array = todepname.Split(',');
            todepid = todepid.Replace(",", "','");
            todepname = todepname.Replace(",", "','");

            //手动—部门
            if (stype == "10")
            {
                //重置
                if (sub == "10")
                {
                    strSql += string.Format("DELETE t_Business_EmpSetting WHERE exists (select userid from chenxin_main.dbo.hr_userinfo where BelongDepartId='{0}' and userid=t_Business_EmpSetting.userid) and DepartId in ('{1}');", depid, todepid);
                    for (int i = 0; i < todepid_array.Length; i++)
                    {
                        if (depid != todepid_array[i])
                            strSql += string.Format("insert into t_Business_EmpSetting(RecordId,CityId,City,CompanyId,Company,DepartId,DepartName,UserId,UserCode,UserName,IsBelong,{0},Addtime) select newid(),'{6}','{7}','{8}','{9}','{1}','{2}',UserId,UserCode,UserName,'0',{3},getdate() from chenxin_main.dbo.Hr_UserInfo where not exists (select UserId from t_Business_EmpSetting where DepartId='{4}' and userid=chenxin_main.dbo.Hr_UserInfo.userid) and BelongDepartId='{5}' and [Status]='1' and Validate=1;", qx, todepid_array[i], todepname_array[i], str_qx.Substring(0, str_qx.Length - 1), todepid_array[i], depid, tocityid, tocity, tocompanyid, tocompany);
                        else
                            strSql += string.Format("insert into t_Business_EmpSetting(RecordId,CityId,City,CompanyId,Company,DepartId,DepartName,UserId,UserCode,UserName,IsBelong,{0},Addtime) select newid(),'{5}','{6}','{7}','{8}','{1}','{2}',UserId,UserCode,UserName,'1',{3},getdate() from chenxin_main.dbo.Hr_UserInfo where BelongDepartId='{4}' and [Status]='1' and Validate=1;", qx, todepid_array[i], todepname_array[i], str_qx.Substring(0, str_qx.Length - 1), depid, tocityid, tocity, tocompanyid, tocompany);
                    }
                }
                //追加
                else if (sub == "20")
                {
                    for (int i = 0; i < todepid_array.Length; i++)
                    {
                        //存在更新
                        strSql += string.Format("update t_Business_EmpSetting set {0} where userid in (select userid from chenxin_main.dbo.hr_userinfo where BelongDepartId='{1}' and [Status]=1 and Validate=1) and departid='{2}';", str_up_t.Substring(0, str_up_t.Length - 1), depid, todepid_array[i]);
                        //不存在插入
                        if (depid != todepid_array[i])
                            strSql += string.Format("insert into t_Business_EmpSetting(RecordId,CityId,City,CompanyId,Company,DepartId,DepartName,UserId,UserCode,UserName,IsBelong,{0},Addtime) select newid(),'{1}','{2}','{7}','{8}','{3}','{4}',UserId,UserCode,UserName,'0',{5},getdate() from chenxin_main.dbo.hr_userinfo where userid not in (select userid from t_Business_EmpSetting where departid='{3}') and [Status]=1 and BelongDepartId='{6}' and Validate=1;", qx, tocityid, tocity, todepid_array[i], todepname_array[i], str_qx.Substring(0, str_qx.Length - 1), depid, tocompanyid, tocompany);
                        else
                            strSql += string.Format("insert into t_Business_EmpSetting(RecordId,CityId,City,CompanyId,Company,DepartId,DepartName,UserId,UserCode,UserName,IsBelong,{0},Addtime) select newid(),'{1}','{2}','{7}','{8}','{3}','{4}',UserId,UserCode,UserName,'1',{5},getdate() from chenxin_main.dbo.hr_userinfo where userid not in (select userid from t_Business_EmpSetting where departid='{3}') and [Status]=1 and BelongDepartId='{6}' and Validate=1;", qx, tocityid, tocity, todepid_array[i], todepname_array[i], str_qx.Substring(0, str_qx.Length - 1), depid, tocompanyid, tocompany);
                    }
                }
                //剔除
                else if (sub == "30")
                {
                    strSql += string.Format("update t_Business_EmpSetting set {0} where userid in (select userid from chenxin_main.dbo.hr_userinfo where BelongDepartId='{1}' and [Status]=1 and Validate=1) and departid in ('{2}');", str_up_f.Substring(0, str_up_f.Length - 1), depid, todepid);
                }

            }
            //手动—员工
            else if (stype == "20")
            {
                string usercode = context.Request.Params["usercode"];
                //重置
                if (sub == "10")
                {
                    strSql += string.Format("delete t_Business_EmpSetting where UserCode='{0}' and DepartId in ('{1}');", usercode, todepid);
                    for (int i = 0; i < todepid_array.Length; i++)
                    {
                        strSql += string.Format("insert into t_Business_EmpSetting(RecordId,CityId,City,CompanyId,Company,DepartId,DepartName,UserId,UserCode,UserName,IsBelong,{0},Addtime) select newid(),'{1}','{2}','{3}','{4}','{5}','{6}',UserId,UserCode,UserName,case (select 1 from chenxin_main.dbo.hr_userinfo where BelongDepartId='{5}' and usercode='{7}' ) when 1 then 1 else 0 end,{8},getdate() from chenxin_main.dbo.Hr_UserInfo where UserCode='{7}';", qx, tocityid, tocity, tocompanyid, tocompany, todepid_array[i], todepname_array[i], usercode, str_qx.Substring(0, str_qx.Length - 1));
                    }
                }
                //追加
                else if (sub == "20")
                {
                    strSql += string.Format("update t_Business_EmpSetting set {0},UserName=(select UserName from chenxin_main.dbo.hr_userinfo where usercode='{1}') where usercode='{1}' and departid in ('{2}');", str_up_t.Substring(0, str_up_t.Length - 1), usercode, todepid);
                    for (int i = 0; i < todepid_array.Length; i++)
                    {
                        strSql += string.Format("insert into t_Business_EmpSetting(RecordId,CityId,City,CompanyId,Company,DepartId,DepartName,UserId,UserCode,UserName,IsBelong,{0},Addtime) select newid(),'{1}','{2}','{3}','{4}','{5}','{6}',UserId,UserCode,UserName,case (select 1 from chenxin_main.dbo.hr_userinfo where BelongDepartId='{5}' and usercode='{7}' ) when 1 then 1 else 0 end,{8},getdate() from chenxin_main.dbo.Hr_UserInfo where UserCode='{7}' and (select count(*) from t_Business_EmpSetting where usercode='{7}' and departid='{5}')<1;", qx, tocityid, tocity, tocompanyid, tocompany, todepid_array[i], todepname_array[i], usercode, str_qx.Substring(0, str_qx.Length - 1));
                    }
                }
                //剔除
                else if (sub == "30")
                {
                    strSql += string.Format("update t_Business_EmpSetting set {0} where usercode='{1}' and departid in ('{2}');", str_up_f.Substring(0, str_up_f.Length - 1), usercode, todepid);
                }
                //如有角色，则进行角色的修改

                //if (role.Length > 0)
                //{
                //    strSql += string.Format("delete Base_RoleUserRelationInfo where UserId=(select UserId from Hr_UserInfo where UserCode='{0}');", usercode);
                //    string[] role_array = role.Split(',');
                //    for (int i = 0; i < role_array.Length; i++)
                //    {
                //        strSql += string.Format("insert into Base_RoleUserRelationInfo select newid(),'{0}',UserId from Hr_UserInfo where UserCode='{1}';", role_array[i], usercode);
                //    }
                //}
            }
            //参照设定
            else if (stype == "30")
            {
                //源对象
                string userid = context.Request.Params["userid"];
                Chenxin.Lib.Passport.Entity.Hr_UserInfo hrinfo = Chenxin.Lib.Passport.DAL.Hr_UserInfo.GetEntityById(new Guid(userid));
                //目标对象
                string aimuserid = context.Request.Params["aimuserid"];

                strSql += string.Format("delete t_Business_EmpSetting where userid='{0}' and IsBelong=0;", userid);
                strSql += string.Format("insert into t_Business_EmpSetting([RecordId],[CityId],[City],[CompanyId],[Company],[DepartId],[DepartName],[UserId],[UserCode],[UserName],[IsBelong],[IsControl],[SearchContractArchive],[EditContractArchive],[CancelContractArchive],[Addtime],SearchIntention,SearchBargain,SearchMoney,SearchBill)"
                        + " select newid(),[CityId],[City],[CompanyId],[Company],[DepartId],[DepartName],'{0}','{1}','{2}',0,[IsControl],[SearchContractArchive],[EditContractArchive],[CancelContractArchive],getdate(),SearchIntention,SearchBargain,SearchMoney,SearchBill "
                       + "from t_Business_EmpSetting where userid='{3}' and DepartId<>'{4}';", hrinfo.UserId, hrinfo.UserCode, hrinfo.UserName, aimuserid, hrinfo.BelongDepartId);

            }

            strSql += "update t_Business_EmpSetting set IsControl='True' where IsBelong='True';";
            bool flag = Chenxin.Lib.Business.DAL.t_Business_EmpSetting.ExecuteSQL(strSql);

            System.Text.StringBuilder str = new System.Text.StringBuilder();

            if (flag)
            {
                //AddLog("" + CurrentUserInfo.UserName + "添加交易设定",
                //    OperationType.OperationTypeComboList()["OprType_AddEMPSetting"].ToString());

                context.Response.Write(string.Format("{{success:'1',msg:'添加成功！',data:''}}"));
            }
            else
            {
                context.Response.Write(string.Format("{{success:'0',msg:'没有可添加的人员',data:''}}"));
            }
        }
        catch (Exception ex)
        {
            context.Response.Write(string.Format("{{success:'0',msg:'意外错误：{0}',data:''}}", ex.Message));
        }
    }
}